﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace WebberCross.JavaScriptBridge.WebParts.JSBridgeDemoWebPart
{
    public partial class JSBridgeDemoUserControl : SilverlightUserControlBase
    {
        protected string _xapSource = "http://spdev010:8080/JavaScriptBridge.SilverlightApplication1.Web/ClientBin/SilverlightApplication1.xap";
        protected string _params = "";
        protected string _jsUniqueClientID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            // To load source from SharePoint web.config insert a key/value in the appSettingsBlock
            //this._xapSource = base.GetConfigSetting("silverLightSource");

            SilverlightWebPartBase baseWP = base.WebPart as SilverlightWebPartBase;

            // Check if XAP source is set by web part
            if (baseWP.XAPSource != "")
                _xapSource = baseWP.XAPSource;

            // Get unique client ID for JS bridge
            _jsUniqueClientID = baseWP.JSUniqueClientID;

            // Pass parameters to silverlight app
            Dictionary<string, string> slParams = new Dictionary<string, string>();
            slParams.Add("serviceSource", base.WebPart.ServiceSource);
            slParams.Add("jsUniqueClientID", baseWP.JSUniqueClientID);
            slParams.Add("jsClientID", baseWP.JSClientID);
            slParams.Add("jsClientSourceID", baseWP.JSClientSourceID);
            slParams.Add("startPage", baseWP.StartPage);

            _params = base.CreateSilverlightParams(slParams);

            // Add Javascript to button
            DemoMessage msg = new DemoMessage("Hello from Web Part", baseWP.JSClientID);
            string jsonMsg = JSONHelper.Serialize(msg);
            this.btnJSBridge.Attributes["onclick"] = string.Format("javascript:JSBridgeSend('{0}', '{1}')", baseWP.JSClientID, jsonMsg);
        }
    }
}
